package com.hjtm.easypigfarm.widget;

import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import com.hjtm.easypigfarm.R;
import com.hjtm.easypigfarm.utility.Constants;
import com.hjtm.easypigfarm.utility.ShortMessageUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity {

	private static final String TAG = LoginActivity.class.getSimpleName();
	private LinearLayout mRegister;
	private LinearLayout mLogin;
	private Button mLogin_enter;
	private Button mLogin_cancel;
	private Button mRegister_enter;
	private Button mRegister_cancel;
	private Context mContext;
	private Button mSendVerify;
	private long time;
	private Timer t;
	private TimerTask tt;
	private TextView mTitle;

	private static long LENGTH = 60 * 1000;
	private int test_flag = 1; // 0 Thread, 1 AsyncTask
	private boolean mIsVerifyCodeCorrect = false;
	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == 0x1) {
				//String text = "秒后重发";
				String text = mContext.getResources().getString(R.string.delay_show);
				mSendVerify.setText(time / 1000 + text);
				time -= 1000;
				if (time < 0) {
					mSendVerify.setEnabled(true);
					mSendVerify.setText(R.string.delay_show_60);
					clearTimer();
				}
			} else if (msg.what == 0x2) {
				if (test_flag == 0) {
				Thread thread = new Thread(new Runnable() {
					@Override
					public void run() {
						//ShortMessageUtils.getInstance().test_ut();
					}
				});
				thread.start();
				} else {
					new AsyncTask <String, String, HashMap>(){

						@Override
						protected HashMap doInBackground(String... arg0) {
							// TODO Auto-generated method stub
							return null;
							//return ShortMessageUtils.getInstance().getTestResult();
						}
						
						@Override
						protected void onPostExecute(HashMap result) {
							// TBD
							if (true) {
								// correct
								mIsVerifyCodeCorrect = true;
							} else {
								// incorrect
								mIsVerifyCodeCorrect = false;
							}
						}
						
					}.execute();
				}
			}
		};
	};

	View.OnClickListener mClick = new View.OnClickListener() {

		@Override
		public void onClick(View arg0) {
			if (arg0.getId() == R.id.login_enter) {
				// send login info to backend for checking
				// pop out a dialog to wait 10s
				boolean login_check = true;
				if (login_check) {
					Intent intent = new Intent(mContext, MainTabActivity.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					mContext.startActivity(intent);
					finish();
				} else {
					Toast.makeText(mContext, R.string.user_pwd_error, Toast.LENGTH_SHORT).show();
				}
			} else if (arg0.getId() == R.id.login_cancel) {
				finish();
			} else if (arg0.getId() == R.id.register_enter) {
				// send register info to backend for checking
				// pop out a dialog to wait 10s
				// if return true
				boolean register_check = true;
				if (register_check && mIsVerifyCodeCorrect) {
					Intent intent = new Intent(mContext, MainTabActivity.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					mContext.startActivity(intent);
					finish();
				} else {
					Toast.makeText(mContext, R.string.verify_fail_backend_error, Toast.LENGTH_LONG).show();
				}
			} else if (arg0.getId() == R.id.register_cancel) {
				finish();
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		mContext = this;
		mLogin = (LinearLayout) findViewById(R.id.login_layout);
		mRegister = (LinearLayout) findViewById(R.id.register);
		mTitle = (TextView) findViewById(R.id.title);
		Intent intent = getIntent();
		boolean flag = intent.getBooleanExtra(Constants.START_FLAG, false);
		if (flag) {
			// login
			mLogin.setAlpha(1.0f);
			mLogin.setVisibility(View.VISIBLE);
			mRegister.setVisibility(View.GONE);
			mLogin_enter = (Button) findViewById(R.id.login_enter);
			mLogin_cancel = (Button) findViewById(R.id.login_cancel);
			mLogin_enter.setOnClickListener(mClick);
			mLogin_cancel.setOnClickListener(mClick);
			mTitle.setText(R.string.login_title);
		} else {
			// register
			mLogin.setVisibility(View.GONE);
			mRegister.setVisibility(View.VISIBLE);
			mRegister.setAlpha(1.0f);
			mRegister_enter = (Button) findViewById(R.id.register_enter);
			mRegister_cancel = (Button) findViewById(R.id.register_cancel);
			mTitle.setText(R.string.register_title);
			mRegister_enter.setOnClickListener(mClick);
			mRegister_cancel.setOnClickListener(mClick);
			mSendVerify = (Button) findViewById(R.id.send_verify);
			mSendVerify.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					mHandler.sendEmptyMessage(0x02);
					// ShortMessageUtils.getInstance().test_ut();
					// TODO Auto-generated method stub
					// String text = "秒后重发";
					mSendVerify.setText(R.string.delay_show_60);
					time = LENGTH;
					initTimer();
					mSendVerify.setEnabled(false);
					if (t != null) {
						t.schedule(tt, 0, 1000);
					}
				}

			});
		}
	}

	private void initTimer() {
		time = LENGTH;
		t = new Timer();
		tt = new TimerTask() {

			@Override
			public void run() {
				Log.i(TAG, time / 1000 + "");
				mHandler.sendEmptyMessage(0x01);
			}
		};
	}

	private void clearTimer() {
		Toast.makeText(mContext, "计时结束", Toast.LENGTH_SHORT).show();
		if (tt != null) {
			tt.cancel();
			tt = null;
		}
		if (t != null)
			t.cancel();
		t = null;
	}

}